package com.goldmantis.alitrip.controller.authorization;

import com.goldmantis.alitrip.common.ReturnMessage;
import com.goldmantis.alitrip.common.SysPermissionCopyVo;
import com.goldmantis.alitrip.common.annotation.Log;
import com.goldmantis.alitrip.common.sysenum.EnumLogCatagory;
import com.goldmantis.alitrip.common.sysenum.EnumOperationType;
import com.goldmantis.alitrip.pojo.SysUserRole;
import com.goldmantis.alitrip.service.SysUserRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * @Author:Shmily
 * @Description:
 * @Date:Create in 12:31 2018/2/11
 */
@RestController
@Api(description = "用户角色管理")
@RequestMapping("sys/user/role")
public class SysUserRoleController {

    @Autowired
    private SysUserRoleService sysUserRoleService;


    @GetMapping(value = "{userRoleId}")
    @ApiOperation(value = "根据Id获取用户角色", httpMethod = "GET")
    public ResponseEntity<ReturnMessage> getById(
            @ApiParam(value = "主键", required = true) @PathVariable(required = true) Long userRoleId) {
        try {
            return ResponseEntity.ok(ReturnMessage.success(sysUserRoleService.selectById(userRoleId)));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ReturnMessage.failure(e));
        }
    }


    /**
     * 给单角色添加多个用户(roleId、userIds必输)
     *
     * @param sysUserRole
     * @return
     */
    @PostMapping()
    @ApiOperation(value = "给人员添加角色(roleId、userIds必须)", httpMethod = "POST")
    @Log(operationName = "给人员分配角色",logCatagory = EnumLogCatagory.operation,operationType = EnumOperationType.grant)
    public ResponseEntity<ReturnMessage> post(@ApiParam("用户角色对象") @RequestBody @Valid SysUserRole sysUserRole) {
        sysUserRoleService.insertBatch(sysUserRole);
        return ResponseEntity.ok(ReturnMessage.success("保存成功!"));
    }

    /**
     * 此方法是正常方法 角色添加用户，没有分类过滤。只有添加功能
     *
     * @param sysUserRole
     * @return
     */
    @PostMapping("postNomal")
    @ApiOperation(value = "给单角色添加多个用户(roleId、userIds必须)", httpMethod = "POST")
    @Log(operationName = "给单角色分配用户",logCatagory = EnumLogCatagory.operation,operationType = EnumOperationType.grant)
    public ResponseEntity<ReturnMessage> postNomal(@ApiParam("用户角色对象") @RequestBody @Valid SysUserRole sysUserRole) {
        sysUserRoleService.insertBatchNoCategory(sysUserRole);
        return ResponseEntity.ok(ReturnMessage.success("保存成功!"));
    }

    /**
     * 给单角色添加多个用户(userId、roleIds必输
     *
     * @param sysUserRole
     * @return
     */
    @PostMapping("postSingleUserMultiRole")
    @ApiOperation(value = "给单用户添加多个角色(userId、roleIds必输)", httpMethod = "POST")
    public ResponseEntity<ReturnMessage> postSingleUserMultiRole(@ApiParam("用户角色对象") @RequestBody @Valid SysUserRole sysUserRole) {
        try {
            sysUserRoleService.insertBatchSingleUserMultiRole(sysUserRole);
            return ResponseEntity.ok(ReturnMessage.success("保存成功!"));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ReturnMessage.failure(e));
        }
    }

    /**
     * 编辑用户角色
     *
     * @param sysUserRole
     * @return
     */
    @PutMapping()
    @ApiOperation(value = "编辑用户角色", httpMethod = "PUT")
    public ResponseEntity<ReturnMessage> put(@ApiParam("用户角色对象") @RequestBody SysUserRole sysUserRole) {
        try {
            return ResponseEntity.ok(ReturnMessage.success(sysUserRoleService.update(sysUserRole)));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ReturnMessage.failure(e));
        }
    }

    /**
     * 批量删除用户角色
     *
     * @param user_role_ids
     * @return
     */
    @DeleteMapping(value = "{user_role_ids}")
    @ApiOperation(value = "批量删除用户角色", httpMethod = "DELETE")
    public ResponseEntity<ReturnMessage> delete(@ApiParam("用户角色id 逗号隔开（1,2）") @PathVariable String user_role_ids) {
        try {
            List<Object> ids = Arrays.asList(StringUtils.split(user_role_ids, ","));
            return ResponseEntity.ok(ReturnMessage.success(sysUserRoleService.deleteByIds(ids, SysUserRole.class, "userroleid")));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ReturnMessage.failure(e));
        }
    }

    @GetMapping(value = "getByRoleId")
    @ApiOperation(value = "根据角色Id获取用户角色信息", httpMethod = "GET")
    public ResponseEntity<ReturnMessage> getByRoleId(
            @ApiParam(value = "角色主键", required = true) @RequestParam(required = true) long roleId,
            @ApiParam(value = "当前页") @RequestParam(defaultValue = "1") Integer offset,
            @ApiParam(value = "每页显示数量") @RequestParam(defaultValue = "10") Integer limit,
            @ApiParam(value = "人员姓名") @RequestParam(required = false) String fullName,
            @ApiParam(value = "账号") @RequestParam(required = false) String account,
            @ApiParam(value = "工号") @RequestParam(required = false) String jobNumber,
            @ApiParam(value = "岗位名") @RequestParam(required = false) String posName,
            @ApiParam(value = "部门名称") @RequestParam(required = false) String orgName) {
        try {
            return ResponseEntity.ok(ReturnMessage.success(sysUserRoleService.selectByRoleId(roleId, null, offset, limit, fullName, account, jobNumber, posName, orgName)));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ReturnMessage.failure(e));
        }
    }

    @GetMapping(value = "getNoRoleByRoleId")
    @ApiOperation(value = "根据角色Id获取未被分配用户角色信息", httpMethod = "GET")
    public ResponseEntity<ReturnMessage> getNoRoleByRoleId(
            @ApiParam(value = "角色主键", required = true) @RequestParam(required = true) Long roleId,
            @ApiParam(value = "当前页") @RequestParam(defaultValue = "1") Integer offset,
            @ApiParam(value = "每页显示数量") @RequestParam(defaultValue = "10") Integer limit,
            @ApiParam(value = "人员姓名") @RequestParam(required = false) String fullName,
            @ApiParam(value = "账号") @RequestParam(required = false) String account,
            @ApiParam(value = "工号") @RequestParam(required = false) String jobNumber,
            @ApiParam(value = "岗位名") @RequestParam(required = false) String posName,
            @ApiParam(value = "部门名称") @RequestParam(required = false) String orgName) {
        try {
            return ResponseEntity.ok(ReturnMessage.success(sysUserRoleService.selectNoRoleByRoleId(roleId, null, offset, limit, fullName, account, jobNumber, posName, orgName)));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ReturnMessage.failure(e));
        }
    }

    @GetMapping(value = "getByUserId")
    @ApiOperation(value = "根据用户Id获取用户角色信息", httpMethod = "GET")
    public ResponseEntity<ReturnMessage> getByUserId(
            @ApiParam(value = "用户主键", required = true) @RequestParam(required = true) Long userId,
            @ApiParam(value = "当前页") @RequestParam(defaultValue = "1") Integer offset,
            @ApiParam(value = "每页显示数量") @RequestParam(defaultValue = "10") Integer limit,
            @ApiParam(value = "人员姓名") String fullName,
            @ApiParam(value = "账号") String account,
            @ApiParam(value = "工号") String jobNumber,
            @ApiParam(value = "岗位名") String posName,
            @ApiParam(value = "部门名称") String orgName) {
        try {
            return ResponseEntity.ok(ReturnMessage.success(sysUserRoleService.selectByRoleId(null, userId, offset, limit, fullName, account, jobNumber, posName, orgName)));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ReturnMessage.failure(e));
        }
    }

//    @GetMapping(value = "getSelectedInfoBySystemIdAndUserId")
//    @ApiOperation(value = "根据系统Id,用户Id获取已分配用户角色信息", httpMethod = "GET")
//    public ResponseEntity<ReturnMessage> getSelectedInfoBySystemIdAndUserId(
//            @ApiParam(value = "系统Id", required = true) @RequestParam Long systemId,
//            @ApiParam(value = "用户Id", required = true) @RequestParam Long userId) {
//        return ResponseEntity.ok(ReturnMessage.success(sysUserRoleService.getSelectedUserRoleInfoBySystemIdAndUserId(systemId, userId)));
//    }

//    @GetMapping(value = "getUnSelectedInfoBySystemId")
//    @ApiOperation(value = "根据系统Id获取角色信息", httpMethod = "GET")
//    public ResponseEntity<ReturnMessage> getUnSelectedInfoBySystemId(
//            @ApiParam(value = "系统Id", required = true) @RequestParam Long systemId) {
//        return ResponseEntity.ok(ReturnMessage.success(sysUserRoleService.getUnSelectedUserRoleInfoBySystemIdAndUserId(systemId)));
//    }

    /**
     * 根据用户Id，角色Ids 删除用户角色(userId、roleIds必输)
     *
     * @param userId  用户Id
     * @param roleIds 角色列表
     * @return
     */
    @DeleteMapping(value = "deleteByUserIdAndRoleIds")
    @ApiOperation(value = "根据用户Id，角色Ids 删除用户角色", httpMethod = "DELETE")
    public ResponseEntity<ReturnMessage> deleteByUserIdAndRoleIds(
            @ApiParam("用户id") @RequestParam Long userId,
            @ApiParam("角色ids") @RequestParam List<Long> roleIds) {
        sysUserRoleService.deleteByUserIdAndRoleIds(userId, roleIds);
        return ResponseEntity.ok(ReturnMessage.success("删除成功！"));
    }

    @GetMapping(value = "getRolesDetailByUserId")
    @ApiOperation(value = "根据用户Id获取该用户的全部角色，用于展现用户详情", httpMethod = "GET")
    public ResponseEntity<ReturnMessage> getRolesDetailByUserId(
            @ApiParam(value = "用户Id", required = true) @RequestParam Long userId) {
        return ResponseEntity.ok(
                ReturnMessage.success(sysUserRoleService.getRolesByUserId(userId)));
    }

    @GetMapping(value = "getRoleCheckInfoBySystemIdAndUserId")
    @ApiOperation(value = "根据系统Id,用户Id获取用户角色分配信息", httpMethod = "GET")
    public ResponseEntity<ReturnMessage> getRoleCheckInfoBySystemIdAndUserId(
            @ApiParam(value = "系统Id", required = true) @RequestParam Long systemId,
            @ApiParam(value = "用户Id", required = true) @RequestParam Long userId,
            @ApiParam(value = "角色分类", required = true) @RequestParam String category) {
        return ResponseEntity.ok(ReturnMessage.success(sysUserRoleService.getRoleCheckInfoBySystemIdAndUserId(systemId, userId, category)));
    }

    /**
     * 权限复制
     * @return
     */
    @PostMapping(value = "permissionCopy")
    @ApiOperation(value = "权限复制", httpMethod = "POST")
    public ResponseEntity<ReturnMessage> permissionCopy(@RequestBody @Valid SysPermissionCopyVo sysPermissionCopyVo) {
        this.sysUserRoleService.permissionCopy(sysPermissionCopyVo.getOriginUserId(),sysPermissionCopyVo.getTargetUserId(),sysPermissionCopyVo.isCopyRole(),sysPermissionCopyVo.isCopyData());
        return ResponseEntity.ok(ReturnMessage.success("权限复制成功！"));
    }


}
